<template>
  <div class="system-scope-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-scope-search mb15">
        <el-input size="default" placeholder="请输入作用域名称" style="max-width: 180px" v-model="state.tableData.param.scopeName" clearable>
        </el-input>
        <el-select v-model="state.tableData.param.status" placeholder="请选择状态" clearable style="max-width: 180px">
          <el-option label="启用" :value="1"></el-option>
          <el-option label="禁用" :value="0"></el-option>
        </el-select>
        <el-button size="default" type="primary" class="ml10" @click="getTableData">
          <el-icon>
            <ele-Search />
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="success" class="ml10" @click="onOpenAddScope">
          <el-icon>
            <ele-FolderAdd />
          </el-icon>
          新增作用域
        </el-button>
      </div>
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60" />
        <el-table-column prop="scopeName" label="作用域名称" min-width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="displayName" label="显示名称" min-width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="isDefault" label="默认作用域" min-width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.isDefault ? 'success' : 'info'" size="small">
              {{ scope.row.isDefault ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" show-overflow-tooltip>
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'" size="small">
              {{ scope.row.status === 1 ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="claims" label="声明" show-overflow-tooltip>
          <template #default="scope">
            <el-tag v-for="claim in scope.row.claims" :key="claim" size="small" class="mr5 mb5">
              {{ claim }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="150">
          <template #default="scope">
            <el-button size="small" text type="primary" @click="onOpenEditScope(scope.row)">
              <el-icon>
                <ele-Edit />
              </el-icon>
              修改
            </el-button>
            <el-button size="small" text type="danger" @click="onDeleteScope(scope.row)" :disabled="scope.row.isDefault">
              <el-icon>
                <ele-Delete />
              </el-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="onHandleSizeChange"
        @current-change="onHandleCurrentChange"
        class="mt15"
        :pager-count="5"
        :page-sizes="[10, 20, 30]"
        v-model:current-page="state.tableData.param.page"
        background
        v-model:page-size="state.tableData.param.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <ScopeDialog ref="scopeDialogRef" @refresh="getTableData" />
  </div>
</template>

<script setup lang="ts" name="oidcScopes">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { getScopeList, deleteScope } from '/@/api/oidc/scopes';

// 引入组件
const ScopeDialog = defineAsyncComponent(() => import('./dialog.vue'));

// 定义变量内容
const scopeDialogRef = ref();
const state = reactive({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      page: 1,
      size: 10,
      scopeName: '',
      status: '',
    },
  },
});

// 初始化表格数据
const getTableData = () => {
  state.tableData.loading = true;
  getScopeList(state.tableData.param)
    .then((res: any) => {
      state.tableData.data = res.data.items;
      state.tableData.total = res.data.total;
    })
    .finally(() => {
      state.tableData.loading = false;
    });
};

// 打开新增作用域弹窗
const onOpenAddScope = () => {
  scopeDialogRef.value.openDialog();
};

// 打开修改作用域弹窗
const onOpenEditScope = (row: any) => {
  scopeDialogRef.value.openDialog(row);
};

// 删除作用域
const onDeleteScope = (row: any) => {
  if (row.isDefault) {
    ElMessage.warning('默认作用域不能删除');
    return;
  }
  
  ElMessageBox.confirm(`确定要删除作用域 "${row.scopeName}" 吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      deleteScope(row.id).then(() => {
        ElMessage.success('删除成功');
        getTableData();
      });
    })
    .catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.size = val;
  getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.page = val;
  getTableData();
};

// 页面加载时
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-scope-container {
  .system-scope-search {
    display: flex;
    margin-bottom: 10px;
    .el-input,
    .el-select {
      margin-right: 10px;
    }
  }
}
</style>